#include "hplctimeprogramunit.h"
#include <QDebug>

HplcTimeProgramUnit::HplcTimeProgramUnit(QObject *parent)
    : QObject(parent)
{

}

void HplcTimeProgramUnit::setTickPeriod(double period)
{
    m_tickPeriod = period;
}

void HplcTimeProgramUnit::reset()
{
    m_current.time = 0;
    m_progress = 0;
}

void HplcTimeProgramUnit::setUnit(PumpUnit unit)
{
    m_unit = unit;
}

void HplcTimeProgramUnit::setNextUnit(PumpUnit unit)
{
    m_nextUnit = unit;
}

PumpUnit HplcTimeProgramUnit::getCurrent() const
{
    return m_current;
}

double HplcTimeProgramUnit::getProgress() const
{
    return m_progress;
}

bool HplcTimeProgramUnit::isFinished() const
{
    return (m_current.time >= m_delta.time);
}

void HplcTimeProgramUnit::calculateDelta()
{
    m_delta.time = (m_nextUnit.time - m_unit.time); // 单位s

    for (int i = 0; i < HPLC_TIME_PROGRAM_PUMP_COUNT; i++)
        m_delta.percent[i] = m_nextUnit.percent[i] - m_unit.percent[i];

    m_current.flow = m_nextUnit.flow;
    m_current.time = 0;
}

void HplcTimeProgramUnit::calculate()
{
    m_progress = m_current.time / m_delta.time;

    for (int i = 0; i < HPLC_TIME_PROGRAM_PUMP_COUNT; i++)
    {
        m_current.percent[i] = m_unit.percent[i] + m_delta.percent[i] * m_progress;
    }
}

void HplcTimeProgramUnit::onTick()
{
    m_current.time += m_tickPeriod;

    qDebug() << m_current.time << m_delta.time;
    if (m_current.time >= m_delta.time)
    {
        m_current.time = m_delta.time;
    }

    calculate();
}
